package com.example.util.cron.whitelist;

import com.example.util.service.TaskService;
import com.example.util.util.FileFolder;
import com.example.util.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class MaapMobileTask {



    @Autowired
    private TaskService taskService;

    @Value("${dir.read}")
    private String readFileDir;

    @Value("${dir.write}")
    private String fileWriterDir;

    @Value("${dir.num:20}")
    private Integer num;

    @Value("${dir.split:0}")
    private Integer split;


    public final static String splitChar = "/";

    /**
     * 白名单处理开启
     */
    @Scheduled(cron = "${cron.mobile.scan}")
    public void tempWhiteCastJobHandler() {

        log.info("调用临时白名单普通定时任务");
        try {
            //号码文件夹标识，每次扫描其中一个文件夹 八位手机号内容被分成100个文件夹，每个文件夹100个文件，一个文件里10000个内容
            List<String> datas = FileFolder.readFileAsList("/opt/app/rms_fg/util-white/aaa.txt");
            //号段分批标识 TO SEE com.example.util.model.MobileCard
            List<String> datas2 = FileFolder.readFileAsList("/opt/app/rms_fg/util-white/bbb.txt");
            //号段类型
            String cardType = datas2.get(0);
            Integer cardTypeiInt = Integer.valueOf(cardType);
            int i = cardTypeiInt % num;
            int j = cardTypeiInt / num;
            if (i == 0) {
                cardType = (num + split) + "";
                j = j - 1;
            } else {
                cardType = (i + split) + "";
            }
            String writeDir = fileWriterDir + splitChar + "task" + (j + 1);
            //号码文件夹
            String phoneDir = datas.get(0);
            //执行文件扫描操作
            taskService.writeFileV2(readFileDir + splitChar + phoneDir, writeDir, cardType);
            datas.removeAll(datas);
            //任务执行后，处理下次任务标识改变
            datas.add(assertFileDir(phoneDir, datas2.get(0)));
            FileUtil.put2FileDelteOld("aaa.txt", datas, "/opt/app/rms_fg/util-white");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            //抛出异常,代表此次任务未成功WhiteTempCastXxlJob
            throw e;
        }
    }

    /**
     * 本次任务执行后，处理下次任务标识改变
     *
     * @param phoneDir 手机号本批次文件夹
     * @param cardType 号段标识
     * @return
     */
    private String assertFileDir(String phoneDir, String cardType) {

        String substring = phoneDir.substring(0, 1);
        String[] split = phoneDir.split(substring);
        Integer integer = Integer.valueOf(split[1]);
        //等于10，进行下位替换
        if (integer >= 10) {
            char c = substring.charAt(0);
            char c2 = (char) (c + 1);
            String value = String.valueOf(c2) + 1;
            //以a1~j10命名，若为k1则证明循环了一次，要重新开始
            if (value.equals("k1")) {
                log.info("=============三个号段一批次完成，本次号段的标识为:【{}】===================", cardType);
                Integer carde = Integer.valueOf(cardType) + 1;
                List<String> list = new ArrayList<>();
                list.add(carde + "");
                FileUtil.put2FileDelteOld("bbb.txt", list, "/opt/app/rms_fg/util-white");
                return "a1";
            }
            return value;
        }
        return substring + (integer + 1);
    }


}
